package adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import Bean.LearnQuestwo;
import Bean.Problems;

public class LearnMoreLeftAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnLongClickListener, View.OnClickListener {
    private List<LearnQuestwo> mList;
    private Context mContext;
    private RelativeLayout repop;

    public LearnMoreLeftAdapter(Context pContext) {
        mContext = pContext;
        mList = new ArrayList<>();
    }

//    public void setupData(List<LearnQues> learnQueslists) {
//        mList.clear();
//        mList = learnQueslists;
//        notifyDataSetChanged();
//    }

    public void setupData(List<LearnQuestwo> learnQueslists, RelativeLayout repops) {
        mList.clear();
        if(mList !=null){
            mList=learnQueslists;
            repop = repops;
        }
        notifyDataSetChanged();
    }

    private OnItemClickListener mOnItemClickListener = null;
    private OnLongClickListener mOnItemLongClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static interface OnLongClickListener {
        boolean onLongItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnLongItemClickListener(OnLongClickListener listener1) {
        this.mOnItemLongClickListener = listener1;
    }

    @Override
    public int getItemCount() {
        // 返回数据集合大小=position值
        return mList.size();
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        return mOnItemLongClickListener != null && mOnItemLongClickListener.onLongItemClick(v, (int) v.getTag());
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        holder.itemView.setTag(position);
        HeadHolder headHolder = (HeadHolder) holder;
        headHolder.mTvTitle.setText(mList.get(position).getGroup());

        if (mList.get(position).getProblems().size() < 3) {
            headHolder.learn_leftimg.setVisibility(View.GONE);
        }

        CustomGridLayoutManager linearLayoutManager = new CustomGridLayoutManager(mContext);
        linearLayoutManager.setScrollEnabled(false);
        headHolder.recyclerView1.setLayoutManager(linearLayoutManager);
        headHolder.recyclerView1.setHasFixedSize(true);//最重要的这句
        LearnMoreRightAdapter learnMoreRightAdapter = new LearnMoreRightAdapter(mContext);
        headHolder.recyclerView1.setAdapter(learnMoreRightAdapter);
        List<Problems> lists = new ArrayList<>();
        if (mList.get(position).getProblems().size() >2){
            lists.add(mList.get(position).getProblems().get(0));
            lists.add(mList.get(position).getProblems().get(1));
            learnMoreRightAdapter.setupData(lists, repop);
        }else {
            learnMoreRightAdapter.setupData(mList.get(position).getProblems(), repop);
        }
        headHolder.learnleft.setOnClickListener(v -> {
            if (!mList.get(position).isStatus() && mList.get(position).getProblems().size() >2) {
                learnMoreRightAdapter.setnewData(mList.get(position).getProblems());


                learnMoreRightAdapter.notifyDataSetChanged();

                mList.get(position).setStatus(true);
//                LogUtils.d( mList.get(position).getProblems().size()+"--------长度打开true-----------"+mList.get(position).isStatus());
                if (mList.get(position).getProblems().size() >2){
                    headHolder.learn_leftimg.setImageResource(R.mipmap.learn_top);
                }
//                RelativeLayout.LayoutParams paramsa =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,150*mList.get(position).getProblems().size());
//                headHolder.learnleft.setLayoutParams(paramsa);
            } else {
                //右边进行的操作
                if (mList.get(position).getProblems().size() > 2) {
                    List<Problems> listsb = new ArrayList<>();
                    listsb.add(mList.get(position).getProblems().get(0));
                    listsb.add(mList.get(position).getProblems().get(1));
                    learnMoreRightAdapter.setnewData(listsb);
                    learnMoreRightAdapter.notifyDataSetChanged();
                }
                //左边进行的操作
                mList.get(position).setStatus(false);
//                LogUtils.d("+++++关闭False+++++++++++"+mList.get(position).isStatus());
                if (mList.get(position).getProblems().size() >2){
                    headHolder.learn_leftimg.setImageResource(R.mipmap.learn_bottom);
                }
            }
            //这玩意会把自身的adapter的onBindViewHolder从新执行一遍，点击传过去的数据会被覆盖，会被初始值覆盖，数据显示所以不正常，妈的有毒
//            notifyDataSetChanged();
        });
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_learnbottomleftreycleview, parent, false);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
//        view.setOnLongClickListener(this);
        return new HeadHolder(view);
    }

    /**
     * 头布局Holder
     */
    static class HeadHolder extends RecyclerView.ViewHolder {
        private ImageView learn_leftimg;
        private TextView mTvTitle;
        private RelativeLayout learnrel;
        private RelativeLayout learnleft;
        private RecyclerView recyclerView1;
        private View viewright;

        public HeadHolder(View itemView) {
            super(itemView);
            learn_leftimg = (ImageView) itemView.findViewById(R.id.learn_leftimg);
            mTvTitle = (TextView) itemView.findViewById(R.id.learn_lefttxt);
            learnrel = (RelativeLayout) itemView.findViewById(R.id.learnrel);
            learnleft = (RelativeLayout) itemView.findViewById(R.id.learnleft);
            recyclerView1 = (RecyclerView) itemView.findViewById(R.id.learn_rightleftrecycler_view);
//            viewright = (View) itemView.findViewById(R.id.viewright);
        }
    }
}